package com.jinxin.platform.cdacscontrolmgr.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.api.annotation.APCOSApi;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdacscontrolmgr.bean.CdparkmanagerProject;
import com.jinxin.platform.cdacscontrolmgr.bean.CdyardmgrChargeConfig;
import com.jinxin.platform.cdacscontrolmgr.bean.CdyardmgrEmplayee;
import com.jinxin.platform.cdacscontrolmgr.bean.bo.OptionItem;
import com.jinxin.platform.cdacscontrolmgr.common.Constant;
import com.jinxin.platform.cdacscontrolmgr.common.SysOrgInfo;
import com.jinxin.platform.cdacscontrolmgr.rpc.CdacscontrolmgrRpc;
import com.jinxin.platform.cdacscontrolmgr.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-09-06
 * @Author: Lingnan
 **/
@APCOSApi(value = "门禁我的项目接口", developers = {"lingnan"}, description = "门禁我的项目接口")
@RestController
@RequestMapping("/cdacscontrolmgr/cdacscontrolmgrMyProject")
@Slf4j
public class CdacscontrolmgrMyProjectController {
    @Autowired
    private ICdacscontrolmgrProjectService iCdacscontrolmgrProjectService;
    @Autowired
    private ICdacscontrolmgrEmployeeService cdacscontrolmgrEmployeeService;
    @Autowired
    private CdacscontrolmgrRpc cdacscontrolmgrRpc;
    @Autowired
    private ICdacscontrolmgrFeeruleService cdacscontrolmgrFeeruleService;
    @Autowired
    private ICdacscontrolmgrChargeConfigService cdacscontrolmgrChargeConfigService;

    @APCOSApiOperation(value = "分页查询项目", developers = {"lingnan"}, description = "分页查询项目",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/list")
    public Map<String,Object> list(@RequestParam int page, @RequestParam int limit,
                                   @RequestParam(required = false,name = "projectName")String projectName,
                                   @RequestParam(required = false,name = "projectNo")String projectNo){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        SysOrgInfo currentOrg = cdacscontrolmgrRpc.getCurrentOrg();
        if(currentOrg == null){
            resultMap.put("msg","获取当前组织信息失败");
            resultMap.put("data", null);
            return resultMap;
        }
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        //param.put("projectStatus", Constant.PROJECT_STATUS_ENABLED);
        param.put("projectType", Constant.PROJECT_TYPE_ACS);
        param.put("projectName", projectName);
        param.put("projectNo", projectNo);
        param.put("organId", currentOrg.getOrgId());
        resultMap.put("count", iCdacscontrolmgrProjectService.listPageCount(param));
        resultMap.put("data", iCdacscontrolmgrProjectService.listPage(param));
        return resultMap;
    }

    @APCOSApiOperation(value = "保存或更新项目", developers = {"lingnan"}, description = "保存或更新项目",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody CdparkmanagerProject record){
        return iCdacscontrolmgrProjectService.saveOrUpdate(record);
    }

    @APCOSApiOperation(value = "删除项目", developers = {"lingnan"}, description = "删除项目",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/delete")
    public JsonResult delete(String id){
        return iCdacscontrolmgrProjectService.delete(id);
    }

    @APCOSApiOperation(value = "查询所有项目", developers = {"lingnan"}, description = "查询所有项目",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/listAll")
    public JsonResult listAll(){
        return JsonResult.ok(iCdacscontrolmgrProjectService.list(new HashMap()));
    }

    @APCOSApiOperation(value = "根据ID查询项目", developers = {"lingnan"}, description = "根据ID查询项目",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/detail")
    public JsonResult detail(String id){
        return iCdacscontrolmgrProjectService.getByid(id);
    }

    // 项目员工相关接口
    @APCOSApiOperation(value = "项目员工相关接口", developers = {"lingnan"}, description = "项目员工相关接口",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/listEmployee")
    public Map<String,Object> listEmployee(@RequestParam(name = "yardId")String yardId){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        if(StringUtils.isEmpty(yardId)){
            resultMap.put("msg","车场id为空");
            resultMap.put("data", null);
            return resultMap;
        }
        HashMap param = new HashMap();
        param.put("yardId", yardId);
        List<CdyardmgrEmplayee> list = cdacscontrolmgrEmployeeService.list(param);
        resultMap.put("count", list.size());
        resultMap.put("data", list);
        return resultMap;
    }

    @APCOSApiOperation(value = "获取角色列表", developers = {"lingnan"}, description = "获取角色列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/loadRoleList")
    public JsonResult loadRoleList(){
        JsonResult jsonResult = cdacscontrolmgrRpc.loadRoleList();
        if(jsonResult.getSuccess()) {
            JSONObject jsonObj = JSONObject.parseObject(jsonResult.getData().toString());
            List<OptionItem> roleList =
                    JSONArray.parseArray(jsonObj.get("data").toString(),OptionItem.class);
            return JsonResult.ok("success",roleList);
        }
        return JsonResult.error("获取角色列表失败");
    }

    @APCOSApiOperation(value = "获取人员列表", developers = {"lingnan"}, description = "获取人员列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/loadUserList")
    public JsonResult loadUserList(){
        return cdacscontrolmgrRpc.loadUserList();
    }

    @APCOSApiOperation(value = "删除员工", developers = {"lingnan"}, description = "删除员工",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/deleteEmployee")
    public JsonResult deleteEmployee(String id){
        return cdacscontrolmgrEmployeeService.delete(id);
    }

    @APCOSApiOperation(value = "保存员工", developers = {"lingnan"}, description = "保存员工",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveEmployee")
    public JsonResult saveEmployee(@RequestBody CdyardmgrEmplayee record){
        return cdacscontrolmgrEmployeeService.saveOrUpdate(record);
    }

    //收费规则相关接口
    @APCOSApiOperation(value = "获取收费规则列表", developers = {"lingnan"}, description = "获取收费规则列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/loadChargeRuleList")
    public JsonResult loadChargeRuleList(){
        SysOrgInfo currentOrg = cdacscontrolmgrRpc.getCurrentOrg();
        if(currentOrg == null){
            return null;
        }
        return JsonResult.ok(cdacscontrolmgrFeeruleService.listOptions(currentOrg.getOrgId()));
    }

    @APCOSApiOperation(value = "获取规则", developers = {"lingnan"}, description = "获取规则",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/getRuleByYardId")
    public JsonResult getRuleByYardId(String yardId){
        return JsonResult.ok(getByYardId(yardId));
    }

    @APCOSApiOperation(value = "保存收费配置", developers = {"lingnan"}, description = "保存收费配置",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveCharge")
    public JsonResult saveCharge(@RequestBody CdyardmgrChargeConfig record){
        return cdacscontrolmgrChargeConfigService.saveOrUpdate(record);
    }

    private CdyardmgrChargeConfig getByYardId(String yardId){
        if(StringUtils.isEmpty(yardId)){return null;}
        HashMap param = new HashMap();
        param.put("yardId",yardId);
        return cdacscontrolmgrChargeConfigService.selectOne(param);
    }

    @APCOSApiOperation(value = "GPIO控制(门禁控制)", developers = {"lingnan"}, description = "GPIO控制(门禁控制)",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/gpioControl/{mac}")
    public JsonResult gpioControl(@PathVariable("mac") String mac){
        return cdacscontrolmgrRpc.gpioControl(mac);
    }

}
